<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/20
 * Time: 14:17
 */

namespace App\Rep;


use App\Models\Invoices;
use App\Models\OrderChild;
use Illuminate\Support\Facades\DB;

class NumberExtractorRep
{

    /**
     * 查询可领取订单
     * @param $params
     * @return array
     */
    public function getOrderListByEmployeeCardNumnberRep($params){
        $employeeRep = new EmployeeRep();
        $list = $employeeRep->searchPensionableOrderChildRep($params);
        return ['code'=>0,'data'=>$list,'msg'=>'success'];
    }

//    public function getDataByDayinRep($params){
//        $employee_user_id = $params['user_id'];
//        $empRep = new EmployeeRep();
//        $employeeInfo = $empRep->setEmployeeInfo($params['user_id']);
//        if($employeeInfo['unclaimed_num']<=0){
//            return ['code'=>1017,'data'=>[],'msg'=>'不可领取多个工单'];
//        }
//        $job_type_id = $empRep->getJobTypeIdByEmployeeUserId($employee_user_id);
//        $orderByArr = (new DispatchOrderRep())->getDispatchOrderSortRep();
//        $query = OrderChild::where('order_child.is_del',"<>",1)->where('order.status',1)
//            ->where(function($q)use($employee_user_id,$job_type_id){
//                $q->orwhere(function($q)use($employee_user_id){
//                    //指派
//                    $q->where('order_child.status',3)
//                        ->where('order_child.employee_user_id',$employee_user_id)
//                        ->where('order_child.is_appoint',1);
//                })->orwhere(function($q)use($job_type_id){
//                    $q->where('order_child.job_type_id',$job_type_id)
//                        ->where('order_child.is_epiboly',2)//是否外包
//                        ->where('order_child.is_appoint',2)//是否指派
//                        ->where('order_child.status',3);
//                });
//            })
//            ->join('order', 'order.id', '=', 'order_child.order_id')
//            ->select(
//                'order_child.*',
//                'order.id as orders_id',
//                'order.color_id',
//                'order.customer_id',
//                'order.model_number_id',
//                'order.product_id',
//                'order.status')
//            ->orderBy($orderByArr[0]['field_name'],$orderByArr[0]['sortord'])
//            ->orderBy($orderByArr[1]['field_name'],$orderByArr[1]['sortord'])
//            ->orderBy($orderByArr[2]['field_name'],$orderByArr[2]['sortord'])
//            ->orderBy($orderByArr[3]['field_name'],$orderByArr[3]['sortord']);
//        $query->with(['order_second']);
//        $list = $query->get();
//        foreach($list as $k=>$v){
//            $jobTypeRep = new JobTypeRep();
//            $ProductJobType = $jobTypeRep->getProductJobTypeRep($v->job_type_id,$v->product_id);
//            //p($ProductJobType);
//            if(!$ProductJobType){
//                continue;
//            }
//            $is_restrict = $jobTypeRep->judgeIsRestrict($ProductJobType);
//            //p($is_restrict);
//            //是否限制流程
//            if($is_restrict){
//                //限制流程，判断上个工序是否完成
//                $res = $this->judgeLastProcess($v->order_id,$v->order_second_id,$ProductJobType);
//                if(!$res){
//                    //return ['code'=>1018,'data'=>[],'msg'=>'限制流程，有尚未完成的工序'];
//                    continue;
//                    unset($list[$k]);
//                }
//            }
//        }
//
//        return $list;
//        return ['code'=>0,'data'=>$list,'msg'=>'success'];
//    }

    /**
     * 查询外包订单
     * @param $params
     */
    public function getEpibolyOrderListByEmployeeCardNumnberRep($params){
        $list = OrderChild::where('is_epiboly',1)
            ->with(['job_type','employee','order_second'=>function($q){
                $q->with(['color'=>function($q){
                    $q->select('id','color');
                },'product'=>function($q){
                    $q->select('id','product_name');
                },'model_number'=>function($q){
                    $q->select('id','model_number');
                },'specification'=>function($q){
                    $q->select('id','specification');
                }]);
            },'order'=>function($q){
                $q->with(['color'=>function($q){
                    $q->select('id','color');
                },'product'=>function($q){
                    $q->select('id','product_name');
                },'customer'=>function($q){
                    $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    $q->select('id','model_number');
                },'specification'=>function($q){
                    $q->select('id','specification');
                }]);
            }])
            ->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 打印外包订单
     */
    public function getEpibolyOrderInfoRep($params){
        //p($params['ids']);
        $list = OrderChild::whereIn('id',$params['ids'])
            ->with(['job_type','employee','order_second'=>function($q){
                $q->with(['color'=>function($q){
                    $q->select('id','color');
                },'product'=>function($q){
                    $q->select('id','product_name');
                },'model_number'=>function($q){
                    $q->select('id','model_number');
                },'specification'=>function($q){
                    $q->select('id','specification');
                }]);
            },'order'=>function($q){
                $q->with(['color'=>function($q){
                    $q->select('id','color');
                },'product'=>function($q){
                    $q->select('id','product_name');
                },'customer'=>function($q){
                    $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    $q->select('id','model_number');
                },'specification'=>function($q){
                    $q->select('id','specification');
                }]);
            }])
            ->get()->toArray();
        return $list;
    }

    /**
     * 获取订单详情
     * @param $params
     *
     */
    public function getOrderChildInfoByEmployeeCardNumnberRep($params){
        $user_id = $params['user_id'];
        $query = OrderChild::query();
        $query->where('job_type_id',$params['job_type_id']);
        $query->where('status',2);
        //$query->where('order_second_id',$params['order_second_id']);
        $query->where('order_id',$params['order_id']);
        $query->where(function($q)use($user_id){
            $q->orwhere(function($q)use($user_id){
                //指派
                $q->where('employee_user_id',$user_id)
                    ->where('is_appoint',1);//指派
            })->orwhere(function($q){
                $q->where('is_epiboly',2)//是否外包
                ->where('is_appoint',2);//是否指派
            });
        });
        $query->limit($params['order_count']);
        $order_child_id_arr = $query->pluck('id');
        $querys = OrderChild::query();
        $querys->where('id',$order_child_id_arr[0]);
        $order_child = $querys->with(['order'=>function($q){
            return $q->with(['customer','modelNumber','color','specification']);
        },'employee','job_type','order_second'=>function($q){
            $q->with(['product','model_number','color','specification']);
        }])->first();


        return ['code'=>0,'data'=>['data'=>$order_child,'count'=>$params['order_count'],'order_child_id_arr'=>$order_child_id_arr],'msg'=>'success'];
    }


}
